package com.slz3.business.module.goodsManagement.gooTypeLevel2.steward;

import com.slz3.business.frame.steward.FindJoinSteward;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.goodsManagement.gooTypeLevel2.domain.GooTypeLevel2;
import com.slz3.business.module.goodsManagement.gooTypeLevel2.domain.GooTypeLevel2Condition;
import com.slz3.business.module.goodsManagement.gooTypeLevel2.domain.GooTypeLevel2Factory;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class GooTypeLevel2Specification {

    public static FindJoin findJoin(MultiValueMap<String, String> params) {
        return new FindJoin(params);
    }

    private static class FindJoin implements Specification<GooTypeLevel2> {

        private final GooTypeLevel2Condition condition;
        private MultiValueMap<String, String> params;

        public FindJoin(MultiValueMap<String, String> params) {
            this.params = params;
            this.condition = GooTypeLevel2Factory.newGooTypeLevel2Condition(params);
        }

        @Override
        public Predicate toPredicate(Root<GooTypeLevel2> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

            final List<Predicate> predicateList = new ArrayList<>();

            predicateList.addAll(FindJoinSteward.appoint(root, cb, GooTypeLevel2.class, params));
            UUID serOneId = condition.getOneId();
            if (VE.isNotEmpty(serOneId)){
                predicateList.add(cb.equal(root.get("serOneId"), serOneId));
            }
            String gooTypeName = condition.getGooTypeName();
            if (StringUtils.hasText(gooTypeName)) {
                predicateList.add(cb.like(root.get("serTypeName"), gooTypeName + "%"));
            }



//            predicateList.add(cb.equal(root.get("validFlag").as(ValidFlag.class), ValidFlag.VALID));

            return predicateList.size() > 0 ? cb.and(predicateList.toArray(new Predicate[]{})) : null;

        }

    }

}
